
from selenium import webdriver
import time
import csv
import re
def search_product(key):
    '''
    模拟搜索商品，获取最大页数
    '''

    driver.find_element_by_id('q').send_keys(key)
    driver.find_element_by_class_name('btn-search').click()
    driver.maximize_window()
    time.sleep(15)

def get_product():
    divs=driver.find_elements_by_xpath('//div[@class="items"]/div[@class="item J_MouserOnverReq  "]')
    for div in divs:                    #./取到当前节点 .//跨节点提取
        info=div.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text#商品名称
        price=div.find_element_by_xpath('.//strong').text+'元'#商品价格
        deal=div.find_element_by_xpath('.//div[@class="deal-cnt"]').text #付款人数
        name = div.find_element_by_xpath('.//div[@class="shop"]/a').text#店铺名称
        print(info,price,deal,name,sep='|')
        # with open('data.csv','a',newline="") as filecsv:
        #     csnwriter= csv.writer(filecsv,delimiter=',')
        #     csnwriter.writerow([info,price,deal,name])

def main():
    search_product(keyword)
    get_product()


if __name__=='__main__':
    keyword=input('请输入你要搜索的商品关键字:')
    driver=webdriver.Chrome()
    driver.get('https://www.taobao.com/')
    main()
